package com.example.mywanandroid.home.business;

import com.example.mywanandroid.home.RecyclerViewItem;
import com.example.mywanandroid.home.ViewPagerItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ParseData {
    public static List<ViewPagerItem> parseViewPagerData(String rawData){
        List<ViewPagerItem> res = new ArrayList<>();
        if (rawData == null) return res;
        try {
            JSONObject bannerObject = new JSONObject(rawData);
            JSONArray bannerArray = bannerObject.getJSONArray("data");
            for (int i = 0; i < bannerArray.length(); ++i) {
                res.add(new ViewPagerItem(
                        bannerArray.getJSONObject(i).getString("imagePath"),
                        bannerArray.getJSONObject(i).getString("url")
                ));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static List<RecyclerViewItem> parseRecyclerViewData(String rawData) {
        List<RecyclerViewItem> res = new ArrayList<>();
        if (rawData == null) return res;
        try {
            JSONObject articleObject = new JSONObject(rawData);
            JSONArray articleArray = articleObject.getJSONObject("data")
                    .getJSONArray("datas");
            for (int i = 0; i < articleArray.length(); ++i) {
                JSONObject artObject = articleArray.getJSONObject(i);
                res.add(new RecyclerViewItem(
                        artObject.getString("author"),
                        artObject.getString("title"),
                        artObject.getString("niceDate"),
                        artObject.getString("link")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
